const initialState = {
  channel_id: 0,
  alllist: [] as channelsType[],
  userList: [] as channelsType[],
};
export type channelsType = {
  id: number;
  name: string;
};
export type channlesAcrtionType =
  | { type: 'channel/savechannels'; payload: channelsType[] }
  | { type: 'channel/saveUserChannels'; payload: channelsType[] }
  | { type: 'channel/updateUserChannels'; payload: channelsType }
  | { type: 'channel/deletUserChannels'; payload: number }
  | { type: 'channel/setChannelId'; payload: number };
export default function channelreduce(
  state = initialState,
  action: channlesAcrtionType
): typeof initialState {
  switch (action.type) {
    case 'channel/savechannels':
      return {
        ...state,
        alllist: action.payload,
      };
    case 'channel/saveUserChannels':
      return {
        ...state,
        userList: action.payload,
      };
    case 'channel/updateUserChannels':
      return {
        ...state,
        userList: [...state.userList, action.payload],
      };
    case 'channel/deletUserChannels':
      return {
        ...state,
        userList: state.userList.filter((item) => {
          return item.id !== action.payload;
        }),
      };
    case 'channel/setChannelId':
      return {
        ...state,
        channel_id: action.payload,
      };
    default:
      return state;
  }
}
